package com.sls.game.supply.deal;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.http.HttpClient;
import com.sls.core.socket.Head;
import com.sls.core.util.DataConversion;
import com.sls.core.util.PropertiesUtil;
import com.sls.core.util.UtilMD5Encryption;
import com.sls.game.common.DeductionRefund;
import com.sls.game.dao.DownstreamUserDAO;
import com.sls.game.dao.GoodsInfoManageDAO;
import com.sls.game.dao.GoodsOrderDAO;
import com.sls.game.dao.GoodsOrderLogDAO;
import com.sls.game.dao.GoodsSupplyManageDAO;
import com.sls.game.dao.SpecialPriceSystemDAO;
import com.sls.game.dao.enfu.EnfuAccountTypeDAO;
import com.sls.game.dao.enfu.EnfuAreaDAO;
import com.sls.game.dao.enfu.EnfuChargeTypeDAO;
import com.sls.game.dao.enfu.EnfuChoosegemDAO;
import com.sls.game.dao.enfu.EnfuGoodsDAO;
import com.sls.game.dao.enfu.EnfuGoodsDetailDAO;
import com.sls.game.dao.enfu.EnfuServerDAO;
import com.sls.game.parser.enfu.EnfuTradeResponse;
import com.sls.game.pojo.DeductionRefundInfo;
import com.sls.game.pojo.GoodsOrderInfo;
import com.sls.game.pojo.GoodsSupplyManageInfo;
import com.sls.game.pojo.SLSHttpInterfaceInfo;
import com.sls.game.pojo.enfu.EnfuAreaInfo;
import com.sls.game.pojo.enfu.EnfuGoodsDetailInfo;
import com.sls.game.pojo.enfu.EnfuServerInfo;
import com.sls.game.pojo.enfu.EnfuTradeResponseInfo;

/**
 * 
 * @description 恩付交易处理
 * @author Wangxuegang
 * @date 2013-5-3
 */
public class EnfuTradeDeal {

	private static Logger log = Logger.getLogger(EnfuTradeDeal.class);

	/**
	 * 
	 * @param info
	 * @param originalGoodsId
	 * @param orderId
	 * @param head
	 * @param orderTime
	 * @param enfuGoodsDAO
	 * @param enfuAccountTypeDAO
	 * @param enfuChargeTypeDAO
	 * @param enfuAreaDAO
	 * @param enfuChoosegemDAO
	 * @param enfuServerDAO
	 * @param enfuGoodsDetailTopDAO
	 * @param goodsOrderDAO
	 * @param goodsOrderLogDAO
	 * @param downstreamUserDAO
	 * @param goodsInfoManageDAO
	 * @param specialPriceSystemDAO
	 * @param paymentOrderNo
	 * @param gson
	 * @return
	 */
	public static String dealTradeOfEnfu(SLSHttpInterfaceInfo info,
			String originalGoodsId, String orderId, Head head,
			String orderTime, EnfuGoodsDAO enfuGoodsDAO,
			EnfuAccountTypeDAO enfuAccountTypeDAO,
			EnfuChargeTypeDAO enfuChargeTypeDAO, EnfuAreaDAO enfuAreaDAO,
			EnfuChoosegemDAO enfuChoosegemDAO, EnfuServerDAO enfuServerDAO,
			GoodsOrderDAO goodsOrderDAO, GoodsOrderLogDAO goodsOrderLogDAO,
			DownstreamUserDAO downstreamUserDAO,
			GoodsInfoManageDAO goodsInfoManageDAO,
			SpecialPriceSystemDAO specialPriceSystemDAO, String paymentOrderNo,
			EnfuGoodsDetailDAO enfuGoodsDetailDAO, String merchantIP,
			GoodsSupplyManageDAO goodsSupplyManageDAO) {
		// 商户号
		String merchantid = PropertiesUtil.getValue("enfu.server.merchantid",
				"enfu.properties").trim();
		updateGoodsOrderOfRegisterAccount(orderId, merchantid, goodsOrderDAO,
				head);

		EnfuTradeResponseInfo resInfo = null;
		resInfo = tradeOfEnfu(info, originalGoodsId, orderId, head, orderTime,
				enfuGoodsDAO, enfuAccountTypeDAO, enfuChargeTypeDAO,
				enfuAreaDAO, enfuChoosegemDAO, enfuServerDAO, goodsOrderDAO,
				enfuGoodsDetailDAO, paymentOrderNo, downstreamUserDAO,
				merchantIP, goodsSupplyManageDAO);
		// 返回给上层对象
		SLSHttpInterfaceInfo sLSHttpInterfaceInfo = new SLSHttpInterfaceInfo();
		String resultcode = null;
		if (resInfo != null) {
			resultcode = resInfo.getResultcode(); // 返回码
			if ("0".equals(resultcode)) { // 处理中
				sLSHttpInterfaceInfo.setResMsg("SUCCESS");
				sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
				sLSHttpInterfaceInfo.setStatus("ORDER_UNDERWAY");
				sLSHttpInterfaceInfo.setSupOrderNo(orderId);
			} else { // 失败
				sLSHttpInterfaceInfo.setResMsg("FAILED");
				sLSHttpInterfaceInfo.setFailedCode("208");
				sLSHttpInterfaceInfo.setFailedReason("充值失败");
				sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
				sLSHttpInterfaceInfo.setStatus("ORDER_FAILED");
				sLSHttpInterfaceInfo.setSupOrderNo(orderId);

				// ====================订单失败，更新订单记录
				GoodsOrderInfo goInfo = new GoodsOrderInfo();
				goInfo.setStatus("0"); // 订单失败
				// 更新订单
				updateGoodsOrder(orderId, head, goInfo, goodsOrderDAO);

				// ==================根据退款情况，更新订单记录
				GoodsOrderInfo goodsOrderInfo = new GoodsOrderInfo();
				DeductionRefundInfo dedInfo = null;
				String paymentStatus = "";
				// 退款
				dedInfo = DeductionRefund.invokeDeductRefund(info, orderId,
						"1", orderTime, paymentOrderNo, head, goodsOrderDAO);
				if (dedInfo != null) {
					paymentStatus = dedInfo.getPaymentStatus();
				}
				if ("1".equals(paymentStatus)) { // 成功
					goodsOrderInfo.setIsPay("3"); // 退款成功
					// 退款成功，通知下游用户
					DeductionRefund.noticeUserRefundMsg(orderId, dedInfo,
							goodsOrderDAO, downstreamUserDAO, null);
				} else if ("2".equals(paymentStatus)) {
					goodsOrderInfo.setIsPay("2"); // 退款失败
				}
				try {
					// 更新订单
					updateGoodsOrder(orderId, head, goodsOrderInfo,
							goodsOrderDAO);
				} catch (Exception e) {
					log.error("请求ID：" + head.getRequestId() + "根据退款状态更新订单异常！",
							e);
				}
			}
		} else { // 存疑
			GoodsOrderInfo or = new GoodsOrderInfo();
			or.setStatus("3");
			updateGoodsOrder(orderId, head, or, goodsOrderDAO);

			sLSHttpInterfaceInfo.setResMsg("SUCCESS");
			sLSHttpInterfaceInfo.setFailedCode("210");
			sLSHttpInterfaceInfo.setFailedReason("订单存疑");
			sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
			sLSHttpInterfaceInfo.setStatus("ORDER_DUBIOUS");
			sLSHttpInterfaceInfo.setSupOrderNo(orderId);
		}
		sLSHttpInterfaceInfo.setGameAccount(info.getGameAccount());
		sLSHttpInterfaceInfo.setUnitPrice(info.getUnitPrice());
		sLSHttpInterfaceInfo.setBuyNum(info.getBuyNum());

		String totalPrice = info.getTotalPrice();
		sLSHttpInterfaceInfo.setTotalPrice(totalPrice);
		Gson gson = CommonUtil.gson();
		String resultBody = gson.toJson(sLSHttpInterfaceInfo);
		return resultBody;
	}

	/**
	 * 更新本地订单
	 * 
	 * @param orderId
	 * @param merchantid
	 * @param goodsOrderDAO
	 * @param head
	 */
	private static void updateGoodsOrderOfRegisterAccount(String orderId,
			String merchantid, GoodsOrderDAO goodsOrderDAO, Head head) {
		GoodsOrderInfo goodsOrderInfo = new GoodsOrderInfo();
		goodsOrderInfo.setOrderId(orderId);
		goodsOrderInfo.setRegisterAccount(merchantid); // 工号
		// 数据更新
		try {
			goodsOrderDAO.update(goodsOrderInfo);
		} catch (Exception e) {
			log.error("处理[" + head.getRequestId() + "]的请求出现异常！", e);
		}
	}

	/**
	 * 
	 * @param info
	 * @param originalGoodsId
	 * @param orderId
	 * @param head
	 * @param orderTime
	 * @param enfuGoodsDAO
	 * @param enfuAccountTypeDAO
	 * @param enfuChargeTypeDAO
	 * @param enfuAreaDAO
	 * @param enfuChoosegemDAO
	 * @param enfuServerDAO
	 * @param enfuGoodsDetailTopDAO
	 * @param goodsOrderDAO
	 * @param goodsOrderLogDAO
	 * @return
	 */
	private static EnfuTradeResponseInfo tradeOfEnfu(SLSHttpInterfaceInfo info,
			String originalGoodsId, String orderId, Head head,
			String orderTime, EnfuGoodsDAO enfuGoodsDAO,
			EnfuAccountTypeDAO enfuAccountTypeDAO,
			EnfuChargeTypeDAO enfuChargeTypeDAO, EnfuAreaDAO enfuAreaDAO,
			EnfuChoosegemDAO enfuChoosegemDAO, EnfuServerDAO enfuServerDAO,
			GoodsOrderDAO goodsOrderDAO, EnfuGoodsDetailDAO enfuGoodsDetailDAO,
			String paymentOrderNo, DownstreamUserDAO downstreamUserDAO,
			String merchantIP, GoodsSupplyManageDAO goodsSupplyManageDAO) {
		// 商品编号
		String productid = null;
		// 商品游戏编号
		String gameid = null;
		// 游戏名称
		String gamename = null;
		// 充值方式id
		String chargetypeid = null;
		// 帐户类型id
		String accounttypeid = null;
		// 恩付商品单价
		String buyprice = null;
		// 充值区域id
		String areaid = null;
		// 充值服务器id
		String srvid = null;
		// 自定义原始商品编号
		String definedproductid = originalGoodsId;
		// 商户号
		String merchantid = PropertiesUtil.getValue("enfu.server.merchantid",
				"enfu.properties").trim();
		// 去商品详细表查询
		EnfuGoodsDetailInfo enfuGoodsDetailInfo = enfuGoodsDetailDAO
				.queryGoodsDetail(definedproductid);
		if (enfuGoodsDetailInfo != null) {
			productid = enfuGoodsDetailInfo.getProductid();
			gamename = enfuGoodsDetailInfo.getGamename();
			chargetypeid = enfuGoodsDetailInfo.getChargetypeid();
			accounttypeid = enfuGoodsDetailInfo.getAccounttypeid();
			gameid = enfuGoodsDetailInfo.getGameid();
		}
		// 去供应表，查询商品单价
		GoodsSupplyManageInfo enfuGoodsInfo = goodsSupplyManageDAO
				.queryByOriginalGoodsId(definedproductid);
		if (enfuGoodsInfo != null) {
			int inPrice = enfuGoodsInfo.getGoodsCostPrice(); // 单位：厘
			String inPriceStr = DataConversion.converIntegerToString(inPrice
					+ "");
			buyprice = inPriceStr;
		}
		// 根据gameid和areaname查询areaid的值
		EnfuAreaInfo areaInfo = new EnfuAreaInfo();
		areaInfo.setProductid(productid);
		areaInfo.setAreaname(info.getGameArea());
		areaInfo.setGamename(gamename);
		EnfuAreaInfo enfuAreaInfo = enfuAreaDAO.queryEnfuAreaInfo(areaInfo);
		if (enfuAreaInfo != null) {
			areaid = enfuAreaInfo.getAreaid();
		}
		// 根据gameid和areaname、srvname查询srvid的值
		EnfuServerInfo srvInfo = new EnfuServerInfo();
		srvInfo.setProductid(productid);
		srvInfo.setAreaname(info.getGameArea());
		srvInfo.setSrvname(info.getGameServer());
		EnfuServerInfo enfuServerInfo = enfuServerDAO
				.queryEnfuServerInfo(srvInfo);
		if (enfuServerInfo != null) {
			srvid = enfuServerInfo.getSrvid();
		}

		String sign = getSignOfTrade(info, orderId, enfuGoodsDAO, merchantid,
				accounttypeid, chargetypeid, buyprice, gameid, gamename,
				productid, areaid, srvid, merchantIP);
		String url = getUrlOfTrade(info, merchantid, productid, buyprice,
				orderId, gamename, gameid, chargetypeid, accounttypeid, areaid,
				srvid, sign, merchantIP);
		EnfuTradeResponseInfo resInfo = sendMsgOfTrading(url, head);
		return resInfo;
	}

	/**
	 * 更新订单
	 * 
	 * @param orderId
	 * @param head
	 * @param info
	 * @param goodsOrderDAO
	 */
	private static void updateGoodsOrder(String orderId, Head head,
			GoodsOrderInfo info, GoodsOrderDAO goodsOrderDAO) {
		info.setOrderId(orderId);
		info.setSynOrAsy("0"); // 同步通知标识
		// 数据更新
		try {
			goodsOrderDAO.update(info);
		} catch (Exception e) {
			log.error(
					"updateGoodsOrder处理[" + head.getRequestId() + "]的请求出现异常！",
					e);
		}
	}

	/**
	 * 发送交易消息
	 * 
	 * @param url
	 * @param head
	 * @return
	 */
	private static EnfuTradeResponseInfo sendMsgOfTrading(String url, Head head) {
		log.info("请求ID：" + head.getRequestId() + "，发送到恩付的请求url：" + url);
		HttpClient httpClient = new HttpClient();
		String supplyEncode = "GB2312";
		try {
			String xmlStr = httpClient.doGet(url, supplyEncode);
			log.info("请求ID：" + head.getRequestId() + "，恩付交易接口响应结果：" + xmlStr);

			EnfuTradeResponseInfo resInfo = EnfuTradeResponse
					.parseTrade(xmlStr);
			return resInfo;
		} catch (Exception e) {
			log.error("请求ID：" + head.getRequestId() + "，恩付交易请求异常！", e);
		}
		return null;
	}

	/**
	 * 获取交易url
	 * 
	 * @param info
	 * @param merchantid
	 * @param productid
	 * @param buyprice
	 * @param orderno
	 * @param gamename
	 * @param gameid
	 * @param chargetypeid
	 * @param accounttypeid
	 * @param areaid
	 * @param srvid
	 * @param sign
	 * @return
	 */
	private static String getUrlOfTrade(SLSHttpInterfaceInfo info,
			String merchantid, String productid, String buyprice,
			String orderno, String gamename, String gameid,
			String chargetypeid, String accounttypeid, String areaid,
			String srvid, String sign, String merchantIP) {
		// 购买数量
		String buynumber = info.getBuyNum();
		// 最终购买商品的用户Ip
		String userip = merchantIP;
		// 充值的玩家用户名
		String username = info.getGameAccount();
		// 充值方式名称
		String chargetypename = info.getChargeTypeName();
		// 帐户类型名称
		String accounttypename = info.getAccountTypeName();
		// 充值区域name
		String areaname = info.getGameArea();
		// 充值服务器name
		String srvname = info.getGameServer();
		// 交易地址
		String address = PropertiesUtil.getValue("enfu.server.address.trade",
				"enfu.properties").trim();
		StringBuilder sb = new StringBuilder();
		sb.append(address);
		sb.append("?");
		sb.append("merchantid=");
		sb.append(merchantid);
		sb.append("&");
		sb.append("productid=");
		sb.append(productid);
		sb.append("&");
		sb.append("buynumber=");
		sb.append(buynumber);
		sb.append("&");
		sb.append("buyprice=");
		sb.append(buyprice);
		sb.append("&");
		sb.append("orderno=");
		sb.append(orderno);
		sb.append("&");
		sb.append("userip=");
		sb.append(userip);
		try {
			if (username != null && !("".equals(username))) {
				username = URLEncoder.encode(username, "GB2312");
				sb.append("&");
				sb.append("username=");
				sb.append(username);
			}
			if (gamename != null && !("".equals(gamename))) {
				gamename = URLEncoder.encode(gamename, "GB2312");
				sb.append("&");
				sb.append("gamename=");
				sb.append(gamename);
			}
			if (gameid != null && !("".equals(gameid))) {
				gameid = URLEncoder.encode(gameid, "GB2312");
				sb.append("&");
				sb.append("gameid=");
				sb.append(gameid);
			}
			if (chargetypename != null && !("".equals(chargetypename))) {
				chargetypename = URLEncoder.encode(chargetypename, "GB2312");
				sb.append("&");
				sb.append("chargetypename=");
				sb.append(chargetypename);
			}
			if (chargetypeid != null && !("".equals(chargetypeid))) {
				chargetypeid = URLEncoder.encode(chargetypeid, "GB2312");
				sb.append("&");
				sb.append("chargetypeid=");
				sb.append(chargetypeid);
			}
			if (accounttypename != null && !("".equals(accounttypename))) {
				accounttypename = URLEncoder.encode(accounttypename, "GB2312");
				sb.append("&");
				sb.append("accounttypename=");
				sb.append(accounttypename);
			}
			if (accounttypeid != null && !("".equals(accounttypeid))) {
				accounttypeid = URLEncoder.encode(accounttypeid, "GB2312");
				sb.append("&");
				sb.append("accounttypeid=");
				sb.append(accounttypeid);
			}
			if (areaname != null && !("".equals(areaname))) {
				areaname = URLEncoder.encode(areaname, "GB2312");
				sb.append("&");
				sb.append("areaname=");
				sb.append(areaname);
			}
			if (areaid != null && !("".equals(areaid))) {
				areaid = URLEncoder.encode(areaid, "GB2312");
				sb.append("&");
				sb.append("areaid=");
				sb.append(areaid);
			}
			if (srvname != null && !("".equals(srvname))) {
				srvname = URLEncoder.encode(srvname, "GB2312");
				sb.append("&");
				sb.append("srvname=");
				sb.append(srvname);
			}
			if (srvid != null && !("".equals(srvid))) {
				srvid = URLEncoder.encode(srvid, "GB2312");
				sb.append("&");
				sb.append("srvid=");
				sb.append(srvid);
			}
		} catch (UnsupportedEncodingException e) {
			log.error(e);
		}
		sb.append("&");
		sb.append("sign=");
		sb.append(sign);

		String url = sb.toString().trim();
		return url;
	}

	/**
	 * 获取交易签名字符串
	 * 
	 * @param originalGoodsId
	 * @param info
	 * @param orderId
	 * @param enfuGoodsDAO
	 * @param merchantid
	 * @param accounttypeid
	 * @param chargetypeid
	 * @param buyprice
	 * @param gameid
	 * @param gamename
	 * @param productid
	 * @return
	 */
	private static String getSignOfTrade(SLSHttpInterfaceInfo info,
			String orderId, EnfuGoodsDAO enfuGoodsDAO, String merchantid,
			String accounttypeid, String chargetypeid, String buyprice,
			String gameid, String gamename, String productid, String areaid,
			String srvid, String merchantIP) {
		// 购买数量
		String buynumber = info.getBuyNum();
		// 本地平台订单号
		String orderno = orderId;
		// 最终购买商品的用户Ip
		String userip = merchantIP;
		// 充值的玩家用户名
		String username = info.getGameAccount();
		// 充值方式名称
		String chargetypename = info.getChargeTypeName();
		// 帐户类型名称
		String accounttypename = info.getAccountTypeName();
		// 充值区域name
		String areaname = info.getGameArea();
		// 充值服务器name
		String srvname = info.getGameServer();

		StringBuilder sb = new StringBuilder();
		if (accounttypeid != null && !("".equals(accounttypeid))) {
			sb.append(accounttypeid);
		}
		if (accounttypename != null && !("".equals(accounttypename))) {
			sb.append(accounttypename);
		}
		if (areaid != null && !("".equals(areaid))) {
			sb.append(areaid);
		}
		if (areaname != null && !("".equals(areaname))) {
			sb.append(areaname);
		}
		sb.append(buynumber);
		sb.append(buyprice);
		if (chargetypeid != null && !("".equals(chargetypeid))) {
			sb.append(chargetypeid);
		}
		if (chargetypename != null && !("".equals(chargetypename))) {
			sb.append(chargetypename);
		}
		if (gameid != null && !("".equals(gameid))) {
			sb.append(gameid);
		}
		if (gamename != null && !("".equals(gamename))) {
			sb.append(gamename);
		}
		sb.append(merchantid);
		sb.append(orderno);
		sb.append(productid);
		if (srvid != null && !("".equals(srvid))) {
			sb.append(srvid);
		}
		if (srvname != null && !("".equals(srvname))) {
			sb.append(srvname);
		}
		sb.append(userip);
		if (username != null && !("".equals(username))) {
			sb.append(username);
		}
		// 密钥
		String key = PropertiesUtil.getValue("enfu.server.secretKey",
				"enfu.properties").trim();
		sb.append(key);

		String data = sb.toString().trim();
		String sign = UtilMD5Encryption.createMD5EncryptionOfGB2312(data);
		return sign;
	}

	/**
	 * 恩付商品详细
	 * 
	 * @param sLSHttpInterfaceInfo
	 * @param originalGoodsId
	 * @param enfuGoodsDetailDAO
	 * @return
	 */
	public static SLSHttpInterfaceInfo getGoodsDetailOfEnfu(
			SLSHttpInterfaceInfo sLSHttpInterfaceInfo, String originalGoodsId,
			EnfuGoodsDetailDAO enfuGoodsDetailDAO, EnfuAreaDAO enfuAreaDAO,
			EnfuServerDAO enfuServerDAO) {
		String productid = null;
		// 是否有区服
		String isarea = null;
		// 支持充值数量列表
		String buynumberlist = null;
		// 充值数量单位
		String buynumberuntil = null;
		// 充值方式名称
		String chargetypename = null;
		// 帐户类型名称
		String accounttypename = null;
		// 自定义原始商品编号
		String definedproductid = originalGoodsId;

		EnfuGoodsDetailInfo goodsDetail = enfuGoodsDetailDAO
				.queryGoodsDetail(definedproductid);
		if (goodsDetail != null) {
			productid = goodsDetail.getProductid();
			isarea = goodsDetail.getIsarea();
			buynumberlist = goodsDetail.getBuynumberlist();
			buynumberuntil = goodsDetail.getBuynumberuntil();
			chargetypename = goodsDetail.getChargetypename();
			accounttypename = goodsDetail.getAccounttypename();
		}
		if ("true".equals(isarea)) { // 有区服信息
			sLSHttpInterfaceInfo = getAreaInfo(sLSHttpInterfaceInfo, productid,
					enfuAreaDAO);
			sLSHttpInterfaceInfo = getSrvInfo(sLSHttpInterfaceInfo, productid,
					enfuServerDAO);
		}
		sLSHttpInterfaceInfo = getChargeTypeString(buynumberlist,
				buynumberuntil, chargetypename, sLSHttpInterfaceInfo);
		sLSHttpInterfaceInfo.setAccountType(accounttypename);
		sLSHttpInterfaceInfo.setGameUnit(buynumberuntil);

		return sLSHttpInterfaceInfo;
	}

	/**
	 * 
	 * @param buynumberlist
	 * @param buynumberuntil
	 * @param chargetypename
	 * @param accounttypename
	 * @param sLSHttpInterfaceInfo
	 * @return
	 */
	private static SLSHttpInterfaceInfo getChargeTypeString(
			String buynumberlist, String buynumberuntil, String chargetypename,
			SLSHttpInterfaceInfo sLSHttpInterfaceInfo) {
		StringBuilder sb = new StringBuilder();
		if (chargetypename != null && !("".equals(chargetypename))) {
			sb.append(chargetypename);
			sb.append("##");
		} else {
			sb.append("null");
			sb.append("##");
		}
		if (buynumberlist != null && !("".equals(buynumberlist))) {
			sb.append(buynumberlist);
			sb.append("##");
		} else {
			sb.append("1"); // 默认是1个
			sb.append("##");
		}
		if (buynumberuntil != null && !("".equals(buynumberuntil))) {
			sb.append(buynumberuntil);
			sb.append("##");
		} else {
			sb.append("null");
			sb.append("##");
		}
		String value = sb.toString().trim();
		String chargeType = value.substring(0, value.length() - 2);
		sLSHttpInterfaceInfo.setChargeType(chargeType);
		return sLSHttpInterfaceInfo;
	}

	/**
	 * 根据gameid查询区域表
	 * 
	 * @param sLSHttpInterfaceInfo
	 * @param gameid
	 * @param enfuAreaDAO
	 * @return
	 */
	private static SLSHttpInterfaceInfo getAreaInfo(
			SLSHttpInterfaceInfo sLSHttpInterfaceInfo, String productid,
			EnfuAreaDAO enfuAreaDAO) {
		// 根据gameid查询区域表
		List<EnfuAreaInfo> list = enfuAreaDAO.queryEnfuAreaList(productid);
		StringBuilder sb = new StringBuilder();
		if (list != null && list.size() > 0) {
			int length = list.size();
			if (length > 40) { // 防止socket通信消息过长
				length = 40;
			}
			for (int i = 0; i < length; i++) {
				EnfuAreaInfo area = list.get(i);
				String areaname = area.getAreaname();
				if (areaname != null && !("".equals(areaname))) {
					sb.append(areaname);
				}
				sb.append("&&");
			}
			String value = sb.toString().trim();
			String gameAreaName = value.substring(0, value.length() - 2);
			sLSHttpInterfaceInfo.setGameArea(gameAreaName);
		}
		return sLSHttpInterfaceInfo;
	}

	/**
	 * 根据gameid查询游戏服务器表
	 * 
	 * @param sLSHttpInterfaceInfo
	 * @param gameid
	 * @param enfuServerDAO
	 * @return
	 */
	private static SLSHttpInterfaceInfo getSrvInfo(
			SLSHttpInterfaceInfo sLSHttpInterfaceInfo, String productid,
			EnfuServerDAO enfuServerDAO) {
		// 根据gameid查询游戏服务器表
		List<EnfuServerInfo> list = enfuServerDAO
				.queryEnfuServerList(productid);
		StringBuilder sb = new StringBuilder();
		if (list != null && list.size() > 0) {
			int length = list.size();
			if (length > 200) { // 防止socket通信消息过长
				length = 200;
			}
			for (int i = 0; i < length; i++) {
				EnfuServerInfo srv = list.get(i);
				String areaname = srv.getAreaname();
				String srvname = srv.getSrvname();
				if (areaname != null && !("".equals(areaname))) {
					sb.append(areaname);
					sb.append("##");
				} else {
					sb.append("null");
					sb.append("##");
				}
				if (srvname != null && !("".equals(srvname))) {
					sb.append(srvname);
				} else {
					sb.append("null");
				}
				sb.append("&&");
			}
			String value = sb.toString().trim();
			String gameServerName = value.substring(0, value.length() - 2);
			sLSHttpInterfaceInfo.setGameServer(gameServerName);
		}
		return sLSHttpInterfaceInfo;
	}

	// public static void main(String[] args){
	// String value = "西北区##服务器一&&西北区##服务器二&&东南区##服一&&东南区##服二&&";
	// String chargeType = value.substring(0, value.length() - 2);
	// System.out.println(chargeType);
	// }

}
